package com.foosun.core.dao;

import com.foosun.core.util.EncryptUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.foosun.core.base.BaseDao;
import com.foosun.core.entity.User;

@Repository
public class UserDao extends BaseDao<User> {

    @Override
    public void update(User entity) throws Exception {
        encryptPwd(entity);
        super.update(entity);
    }

    @Override
    public void save(User entity) throws Exception {
        encryptPwd(entity);
        super.save(entity);
    }

    private void encryptPwd(User entity) {
        String pwd = entity.getPassword();
        if (StringUtils.isNotBlank(pwd)) {
            if (pwd.length() != 32) {
                pwd = EncryptUtils.md5(pwd);
            }
        }
        entity.setPassword(pwd);
    }
}
